package com.nanyang.tms.manager.company.impl;

import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.company.CompanyDeclareDao;
import com.nanyang.tms.domain.company.CompanyDeclareDO;
import com.nanyang.tms.domain.company.CompanyDeclareQuery;
import com.nanyang.tms.domain.company.CompanyDeclareVO;
import com.nanyang.tms.manager.company.CompanyDeclareManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component("companyDeclareManager")
public class CompanyDeclareManagerImpl implements CompanyDeclareManager {
    @Autowired
    private CompanyDeclareDao companyDeclareDao;
    @Autowired
    private GlobalIdGeneratorUtil globalIdGeneratorUtil;
    @Override
    public PageBean<CompanyDeclareVO> listCompanyDeclare(CompanyDeclareQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = companyDeclareDao.countDeclare(query);
            PageBean<CompanyDeclareVO> page = new PageTableBean<CompanyDeclareVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(companyDeclareDao.listDeclare(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }

    @Override
    public void saveCompanyDeclare(CompanyDeclareDO companyDeclareDO) {
        try {
            companyDeclareDO.setDeclareCode(TmsSerializeConstant.COMPANYDECLARE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COMPANYDECLARE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY));
            companyDeclareDao.insert(companyDeclareDO);
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
}
